package com.bsoft.gol.hcs.clinical.dao;


import com.bsoft.gol.hcs.clinical.bean.req.OutpatientDocRequest;
import ctd.persistence.annotation.DAOMethod;
import ctd.persistence.annotation.DAOParam;
import ctd.persistence.support.hibernate.HibernateSupportDelegateDAO;
import ctd.persistence.support.hibernate.template.AbstractHibernateStatelessResultAction;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import ctd.persistence.support.hibernate.template.HibernateStatelessResultAction;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Query;
import org.hibernate.StatelessSession;
import pcn.consultation.OdsDiagnosis;

import java.util.List;

public abstract class OdsDiagnosisDAO extends HibernateSupportDelegateDAO<OdsDiagnosis> {
    @DAOMethod(sql = "from OdsDiagnosis where consultId =:consultId and deleteFlag='0'")
    public abstract List<OdsDiagnosis> queryOdsDiagnosisListByConsultId(@DAOParam("consultId") String consultId);

    @DAOMethod(sql = " from OdsDiagnosis where consultId =:consultId and deleteFlag = 0")
    public abstract List<OdsDiagnosis> queryOdsDiagnosisByConsultId(@DAOParam("consultId") String consultId);

    @DAOMethod(sql = "delete from OdsDiagnosis where consultId =:consultId")
    public abstract void deleteOdsDiagnosisByConsultId(@DAOParam("consultId") String consultId);

    @DAOMethod(sql = " from OdsDiagnosis where consultId =:consultId and mainFlag =1  and deleteFlag = 0")
    public abstract OdsDiagnosis getMainOdsDiagnosis(@DAOParam("consultId") String consultId);

    @DAOMethod(sql = " from OdsDiagnosis where consultId =:consultId and diagnosisType =:diagnosisType and diseasesId=:diseasesId  and deleteFlag = 0")
    public abstract OdsDiagnosis getExistOdsDiagnosis(@DAOParam("consultId") String consultId, @DAOParam("diagnosisType") String diagnosisType, @DAOParam("diseasesId") Integer diseasesId);

    @DAOMethod(sql = " from OdsDiagnosis where consultId =:consultId and mainFlag =0  and deleteFlag = 0")
    public abstract List<OdsDiagnosis> queryNoMainOdsDiagnosis(@DAOParam("consultId") String consultId);

    @DAOMethod(sql = " from OdsDiagnosis where diseasesId =:diseasesId ")
    public abstract List<OdsDiagnosis> queryOdsDiagnosis(@DAOParam("diseasesId") Integer diseasesId);

    @DAOMethod(sql = " from OdsDiagnosis where consultId =:consultId and deleteFlag = 0")
    public abstract List<OdsDiagnosis> queryOdsDiagnosisList(@DAOParam("consultId") String consultId);

    @DAOMethod(sql = " from OdsDiagnosis where diseasesId =:diseasesId and consultId =:consultId  and deleteFlag = 0 ")
    public abstract List<OdsDiagnosis> queryOdsDiagnosisTwo(@DAOParam("diseasesId") Integer diseasesId,
                                                         @DAOParam("consultId")String consultId,@DAOParam("diagnosisType")String diagnosisType);

    public List<OdsDiagnosis> queryLocalDiagnoseInformation(final OutpatientDocRequest request){
        HibernateStatelessResultAction<List<OdsDiagnosis>> action = new AbstractHibernateStatelessResultAction<List<OdsDiagnosis>>() {
            @Override
            public void execute(StatelessSession ss) {
                StringBuffer stringBuffer = new StringBuffer();
                stringBuffer.append(" from OdsDiagnosis where deleteFlag ='0' ");
                if (StringUtils.isNotEmpty(request.getConsultId())) {
                    stringBuffer.append(" and consultId = '" + request.getConsultId()+"'");
                }
                if (StringUtils.isNotEmpty(request.getConsultType())) {
                    stringBuffer.append(" and consultType = '" + request.getConsultType()+"'");
                }
                stringBuffer.append(" order by diseasesCode ");
                Query sqlQuery = ss.createQuery(stringBuffer.toString());
                setResult(sqlQuery.list());
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }

    @DAOMethod(sql = " update from OdsDiagnosis set deleteFlag=:deleteFlag where diseasesId =:diseasesId and consultId =:consultId ")
    public abstract void updateDeleteFlag(@DAOParam("diseasesId") Integer diseasesId, @DAOParam("deleteFlag") String deleteFlag, @DAOParam("consultId") String consultId);
}
